package com.example.himalaya.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.himalaya.R;
import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.utils.LogUtil;

public abstract class UILoader extends FrameLayout {
    private static final String TAG = "UILoader";
    private View mLoadingView;
    private View mSuccessView;
    private View mNetworkError;
    private View mEmptyView;

    //定义一个枚举类
    public  enum  UIStatus{
        LOADING,SUCCESS,NETWORK_ERROR,EMPTY,NONE
    }

    public  UIStatus mCurrentStatus = UIStatus.NONE;

    public UILoader(@NonNull Context context) {
        this(context,null);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UILoader(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void updateStatus(UIStatus status){
        mCurrentStatus = status;
        //在主线程上更新UI
        BaseApplication.getHandler().post(new Runnable() {
            @Override
            public void run() {
                switchUIBuCurrentStatus();
            }
        });
    }

    /**
     * 初始化UI
     * */
    private void init() {
        switchUIBuCurrentStatus();
    }

    private void switchUIBuCurrentStatus() {
        //加载中
        if (mLoadingView == null) {
            mLoadingView = getLoadingView();
            addView(mLoadingView);
        }
        //根据状态设置是否可见
        mLoadingView.setVisibility(mCurrentStatus == UIStatus.LOADING?VISIBLE:GONE);

        //成功
        if (mSuccessView == null) {
            LogUtil.d(TAG,"switchUIBuCurrentStatus- mSuccessView --> ");
            mSuccessView = getmSuccessView(this);
            addView(mSuccessView);
        }
        //根据状态设置是否可见
        mSuccessView.setVisibility(mCurrentStatus == UIStatus.SUCCESS?VISIBLE:GONE);

        //网络错误
        if (mNetworkError == null) {
            mNetworkError = getmNetworkError();
            addView(mNetworkError);
        }
        //根据状态设置是否可见
        mNetworkError.setVisibility(mCurrentStatus == UIStatus.NETWORK_ERROR?VISIBLE:GONE);

        //数据为空
        if (mEmptyView == null) {
            mEmptyView = getmEmptyView();
            addView(mEmptyView);
        }
        //根据状态设置是否可见
        mEmptyView.setVisibility(mCurrentStatus == UIStatus.EMPTY?VISIBLE:GONE);
    }

    private   View getmEmptyView(){
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view,this,false);
    }

    protected  abstract View getmSuccessView(ViewGroup container);

    private  View getmNetworkError(){
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_error_view,this,false);
    };

    private  View getLoadingView(){
        return LayoutInflater.from(getContext()).inflate(R.layout.fragment_loading_view,this,false);
    };
}
